/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package wso.global;

import com.sun.codemodel.JCodeModel;
import com.sun.tools.xjc.api.ErrorListener;
import com.sun.tools.xjc.api.S2JJAXBModel;
import com.sun.tools.xjc.api.SchemaCompiler;
import com.sun.tools.xjc.api.XJC;
import java.io.File;
import java.io.IOException;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.datatype.DatatypeConfigurationException;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.bind.Marshaller;
import javax.xml.datatype.XMLGregorianCalendar;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;
import wso.dto.WsoCustomer;
import wso.dto.WsoProduct;
import wso.dto.WsoRole;
import wso.dto.WsoUser;
import wso.xml.generate.customer.CustomerType;
import wso.xml.generate.customer.Customers;
import wso.xml.generate.product.ProductType;
import wso.xml.generate.product.Products;
import wso.xml.generate.user.UserType;
import wso.xml.generate.user.Users;

/**
 *
 * @author Violet_Ink
 */
public class CommonUtil {

    public static Date getCurrentDate() {
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.YEAR, 0);
        cal.set(Calendar.MONTH, 0);
        cal.set(Calendar.DATE, 0);
        return cal.getTime();
    }
	
    public static void generateJAXB(String ouputPath, String packageName, String schemaFile) {
        try {
            SchemaCompiler sc = XJC.createSchemaCompiler();
            sc.setErrorListener(new ErrorListener() {

                public void error(SAXParseException saxpe) {
                    System.out.println("ERROR " + saxpe.getMessage());
                }

                public void fatalError(SAXParseException saxpe) {
                    System.out.println("FATAL " + saxpe.getMessage());
                }

                public void warning(SAXParseException saxpe) {
                    System.out.println("WARNING " + saxpe.getMessage());
                }

                public void info(SAXParseException saxpe) {
                    System.out.println("INFO " + saxpe.getMessage());
                }
            });

            sc.forcePackageName(packageName);
            File schema = new File(schemaFile);
            InputSource is = new InputSource(schema.toURI().toString());
            sc.parseSchema(is);
            S2JJAXBModel model = sc.bind();
            JCodeModel code = model.generateCode(null, null);
            code.build(new File(ouputPath));

        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void marshallCustomer(WsoCustomer[] customerArray, File file) {

        ArrayList<CustomerType> cusTypeList = new ArrayList<CustomerType>();
        try {
            for (int i = 0; i < customerArray.length; i++) {
                CustomerType customerType = new CustomerType();
                WsoCustomer cust = customerArray[i];
                customerType.setCustomerid(cust.getCustomerid());
                customerType.setCustomername(cust.getCustomername());
                customerType.setAddress(cust.getAddress());
                customerType.setCity(cust.getCity());
                customerType.setTelephone(cust.getTelephone());
                customerType.setMobile(cust.getMobile());
                customerType.setEmail(cust.getEmail());
                customerType.setNote(cust.getNote());
                XMLGregorianCalendar xmlGregorianCalendar;
                xmlGregorianCalendar = GlobalFunction.ConvertDatetoXMLGregorianCalendar(cust.getBirthdate());
                customerType.setBirthday(xmlGregorianCalendar);
                customerType.setIsdeleted(cust.getIsdeleted());
                cusTypeList.add(customerType);
            }
        
            Customers custs = new Customers();
            custs.getCustomer().addAll(cusTypeList);
            JAXBContext ctx = JAXBContext.newInstance(Customers.class);
            Marshaller mar = ctx.createMarshaller();
            mar.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
            mar.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
            mar.marshal(custs, file);
        } catch (JAXBException e) {
            e.printStackTrace();
        } catch (DatatypeConfigurationException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void marshallProduct(WsoProduct[] productArray, File file) {

        ArrayList<ProductType> proTypeList = new ArrayList<ProductType>();

        for (int i = 0; i < productArray.length; i++) {
            ProductType productType = new ProductType();
            WsoProduct pro = productArray[i];
            productType.setProductid(pro.getProductid());
            productType.setProductname(pro.getProductname());
            productType.setOriginid(pro.getWsoOrigin().getOriginid());
            productType.setProducttypeid(pro.getWsoProductType().getProducttypeid());
            productType.setNote(pro.getNote());
            productType.setImage(pro.getImage());
            productType.setThumb(pro.getThumb());
            productType.setMakeyear(pro.getMakeyear());
            productType.setCapacity(pro.getCapacity());
            productType.setConcentration(pro.getConcentration());
            productType.setLastprice(pro.getLastprice());
            productType.setIsdeleted(pro.getIsdeleted());
            proTypeList.add(productType);
        }

        try {
            Products pros = new Products();
            pros.getProduct().addAll(proTypeList);
            JAXBContext ctx = JAXBContext.newInstance(Products.class);
            Marshaller mar = ctx.createMarshaller();
            mar.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
            mar.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
            mar.marshal(pros, file);

        } catch (JAXBException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void marshallUser(WsoUser[] userArray, File file) {
        ArrayList<UserType> userlist = new ArrayList<UserType>();
        for (int i = 0; i < userArray.length; i++) {
            UserType user = new UserType();
            user.setUsername(userArray[i].getUsername());
            user.setPassword(userArray[i].getPassword());
            user.setRoleid(userArray[i].getWsoRole().getRoleid());
            user.setFullname(userArray[i].getFullname());
            user.setEmail(userArray[i].getEmail());
            user.setAddress(userArray[i].getAddress());
            user.setPhone(userArray[i].getPhone());
            user.setGender(userArray[i].getGender());
            //user.setBirthday(userArray[i].getBirthday());
            user.setIsdeleted(userArray[i].isIsdeleted());
            userlist.add(user);
        }

        try {
            Users users = new Users();
            users.getUser().addAll(userlist);
            JAXBContext ctx = JAXBContext.newInstance(Users.class);
            Marshaller mar = ctx.createMarshaller();
            mar.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
            mar.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
            mar.marshal(users, file);

        } catch (JAXBException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] arg) {
//        generateJAXB("src/java", "wso.xml.generate.customer", "src/java/wso/xml/schema/customers.xsd");
//        generateJAXB("src/java", "wso.xml.generate.order", "src/java/wso/xml/schema/orders.xsd");
//        generateJAXB("src/java", "wso.xml.generate.orderdetail", "src/java/wso/xml/schema/orderdetails.xsd");
//        generateJAXB("src/java", "wso.xml.generate.origin", "src/java/wso/xml/schema/origins.xsd");
//        generateJAXB("src/java", "wso.xml.generate.product", "src/java/wso/xml/schema/products.xsd");
//        generateJAXB("src/java", "wso.xml.generate.producttype", "src/java/wso/xml/schema/producttypes.xsd");
//        generateJAXB("src/java", "wso.xml.generate.role", "src/java/wso/xml/schema/roles.xsd");
//        generateJAXB("src/java", "wso.xml.generate.user", "src/java/wso/xml/schema/users.xsd");
    }
}
